<template>
  <div class="hm-Register">
    <div class="header">面经注册</div>
    <van-form validate-first @failed="onFailed">
      <!-- 通过 pattern 进行正则校验 -->
      <van-field
        v-model="value1"
        name="pattern"
        placeholder="用户名"
        :rules="[{ pattern, message: '请输入用户名' }]"
      />
      <!-- 通过 validator 进行函数校验 -->
      <van-field
        v-model="value2"
        name="validator"
        placeholder="密码"
        :rules="[{ validator, message: '请输入密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          color="#07c160"
          native-type="submit"
          @click="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <div class="footer">已有账号,去登陆</div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "HmRegister",
  data() {
    return {
      value1: "",
      value2: "",
      pattern: /^[\w-]{2,16}$/,
    };
  },
  methods: {
    // 校验函数返回 true 表示校验通过，false 表示不通过
    validator(val) {
      return /\d{6}/.test(val);
    },
    onFailed(errorInfo) {
      console.log("failed", errorInfo);
    },

    // 输入用户名和密码且合规之后 点击提交发请求  注册
    async submit() {
      console.log(this.value1, this.value2);
      if (this.value1 && this.value2) {
        const res = await axios({
          method: "post",
          url: "http://interview-api-t.itheima.net/h5/user/register",
          data: {
            username: this.value1,
            password: this.value2,
          },
        });
        console.log(res);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.hm-login {
  .header {
    width: 100%;
    height: 30px;
    text-align: center;
    font-weight: 500;
  }
}
</style>
